//
//  LocalTRavelBaseRequest.m
//  LocalTravel
//
//  Created by zheng ji on 14-3-23.
//  Copyright (c) 2014年 jxw. All rights reserved.
//

#import "LocalTRavelBaseRequest.h"

@implementation LocalTRavelBaseRequest
/*获取同程api协议header*/
+(NSString*)getRequestHeader:(NSString*) serviceName{
    NSString* key=@"60fa0dce5e7e7775";
    NSString* version=@"20111128102912";
    NSString* accountID=@"563b46d9-7772-491d-9c62-0476949aac47";
    // NSString* serviceName=@"GetSceneryList";
    
    NSDateFormatter* dateFormater=[[NSDateFormatter alloc] init];
    [dateFormater setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
    NSDate* curDate=[[NSDate alloc]init];
    NSString* reqTime=[dateFormater stringFromDate:curDate];
    
    curDate =nil ;
    dateFormater=nil;
    
    NSMutableArray* array=[[NSMutableArray alloc]init];
    [array addObject:[NSString stringWithFormat:@"Version=%@",version]];
    [array addObject:[NSString stringWithFormat:@"AccountID=%@",accountID]];
    [array addObject:[NSString stringWithFormat:@"ServiceName=%@",serviceName]];
    [array addObject:[NSString stringWithFormat:@"ReqTime=%@",reqTime]];
    
    [self bubbleSort:array];
    
    NSMutableString* prestr=[[NSMutableString alloc]init];
    for(int i=0;i<array.count;i++){
        if(i==array.count-1){
            [prestr appendString:[array objectAtIndex:i ]];
        }else{
            [prestr appendFormat:@"%@&",[array objectAtIndex:i]];
        }
    }
    
    [prestr appendString:key];
    
    NSString* md5String=[self md5HexDigest:prestr];
    prestr=nil;
    NSMutableString* requestXml=[[NSMutableString alloc]init];
    
    
    [requestXml appendString:@"<header>"];
    [requestXml appendFormat:@"<version>%@</version>",version];
    [requestXml appendFormat:@"<accountID>%@</accountID>",accountID];
    [requestXml appendFormat:@"<serviceName>%@</serviceName>",serviceName];
    [requestXml appendFormat:@"<digitalSign>%@</digitalSign>",md5String];
    [requestXml appendFormat:@"<reqTime>%@</reqTime>",reqTime];
    [requestXml appendFormat:@"</header>"];
    
    
    return  requestXml   ;
}

+ (NSString *)sendRequest:(NSMutableString *)requestXml{
    return [self sendRequest:requestXml tongChengUrl:nil];
}
+ (NSString *)sendRequest:(NSMutableString *)requestXml  tongChengUrl:(NSString*) tongChengUrl{
    if(tongChengUrl ==nil || [tongChengUrl isEqualToString:@""]) tongChengUrl=[LocalTravelConfig getTongChengApiUrl];
    
    UIApplication* uiApp=[UIApplication sharedApplication];
    uiApp.networkActivityIndicatorVisible=YES;
    
    NSURL* url=[NSURL URLWithString:tongChengUrl];
    NSMutableURLRequest* urlRequest=[[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
    [urlRequest setHTTPMethod:@"POST"];
    [urlRequest setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
    [urlRequest setHTTPBody:[requestXml dataUsingEncoding:NSUTF8StringEncoding]];
    NSData* responseData=[NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:nil];
    
    NSString* returnXml=[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
    
    url =nil;
    urlRequest =nil;
    
    uiApp.networkActivityIndicatorVisible=NO;
    
    return returnXml;
}

/*冒泡排序数组*/
+(void)bubbleSort:(NSMutableArray *)array {
    int i, y;
    BOOL bFinish = YES;
    for (i = 1; i<= [array count] && bFinish; i++) {
        bFinish = NO;
        for (y = (int)[array count]-1; y>=i; y--) {
            NSString *str1 = [array objectAtIndex:y];
            NSString *str2 = [array objectAtIndex:y-1];
            if([str1 compare:str2]==NSOrderedAscending ){
                
                [array exchangeObjectAtIndex:y-1 withObjectAtIndex:y];
                bFinish = YES;
            }
        }
    }
}
/*MD5加密字符串*/
+ (NSString *)md5HexDigest:(NSString*)input
{
    const char* str = [input UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(str, strlen(str), result);
    NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH*2];//
    
    for(int i = 0; i<CC_MD5_DIGEST_LENGTH; i++) {
        [ret appendFormat:@"%02x",result[i]];
    }
    return ret;
}


@end
